﻿using System;
using System.Data;
using System.Windows.Forms;
using System.Threading;
using System.Data.SqlClient;
using DevExpress.XtraEditors;
using DevExpress.XtraReports.UI;
using prjKQXS_DLL;

namespace prjKQXS_UI.View
{
    public partial class frmHDong : XtraForm
    {
        /// <summary>
        /// Giá trị biến này tự động được gán là '%' khi chọn tất cả hoặc là mã đơn vị tương ứng khi chọn cán bộ từ lueDonVi
        /// </summary>
        char MaDV = '%';

        /// <summary>
        /// TenDV (Tên đơn vị) tự động được gán khi chọn cán bộ từ lueDonVi
        /// </summary>
        string TenDV;
        
        public frmHDong()
        {
            InitializeComponent();
        }

        SqlConnection conn = new SqlConnection(BienTc.connString);

        private void simpleButton1_Click(object sender, EventArgs e)
        {            
            marqueeProgressBarControl1.Properties.Stopped = false;
            Thread t = new Thread(new ThreadStart(inbaocao));
            t.SetApartmentState(ApartmentState.STA);
            t.Start();
        }

        private void frmHDong_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            marqueeProgressBarControl1.Properties.Stopped = true;
            loadLueDonVi();
            LoadDmTrangThai();

            ucDonVi1.lueDonVi.EditValueChanged += new EventHandler(lueDonVi_EditValueChanged);
        }

        private void loadLueDonVi()
        {
            ucDonVi1.lueDonVi.Properties.DataSource = clsCommon.load_lueDonVi();

            if (BienTc.isQuanHuyen || BienTc.isThanhPho || !clsCommon.hasPermission(BienTc.UserID, (int)BienTc.eFunction.L_DonVi))
            {
                ucDonVi1.lueDonVi.ItemIndex = ucDonVi1.lueDonVi.Properties.GetDataSourceRowIndex("Ma_DT", BienTc.ma_huyen == "" ? "TC" : BienTc.ma_huyen);
                if (BienTc.isQuanHuyen || BienTc.isThanhPho) ucDonVi1.lueDonVi.Properties.ReadOnly = true;
            }
            else ucDonVi1.lueDonVi.ItemIndex = 0;
        }

        string Ma_Canbo = "";
        string trangthai = "";
        private void inbaocao()
        {
            Ma_Canbo = ucCanBo1.lueCanBo.GetColumnValue("Ma_DT").ToString();
            trangthai = lueTTrang.GetColumnValue("Ma_DT").ToString();
            prjKQXS_Report.rptHopDong rpt = new prjKQXS_Report.rptHopDong(Ma_Canbo, trangthai);
            rpt.ShowRibbonPreviewDialog();
            marqueeProgressBarControl1.Properties.Stopped = true;
        }

        private void loadLueCanBo()
        {
            ucCanBo1.lueCanBo.Properties.DataSource = clsCommon.load_lueCanBo(MaDV);
            if (ucCanBo1.lueCanBo.ItemIndex != 0) ucCanBo1.lueCanBo.ItemIndex = 0;
            //else lueCanBo_EditValueChanged(null, null);
        }

        private void LoadDmTrangThai()
        {
            try
            {
                if (conn.State == ConnectionState.Closed)conn.Open();
                DataTable dt = clsCommon.FillData("Select Ma_DT +' - '+Ten_DT as Ten, * from DMTINHTRANG");
                DataRow row = dt.NewRow();
                row["Ma_DT"] = "TC";
                row["Ten_DT"] = "Công ty";
                row["Ten"] = "Công ty";
                dt.Rows.InsertAt(row, 0);
                lueTTrang.Properties.DataSource = dt;
                if (dt.Rows.Count > 0)lueTTrang.ItemIndex = 0;
            }
            catch (Exception)
            {
                clsCommon.MsgBox("Không thể kết nối tới máy chủ", 0);
            }
            finally
            {
                if (conn.State == ConnectionState.Open)conn.Close();
            }
        }

        private void lueDonVi_EditValueChanged(object sender, EventArgs e)
        {
            string madv = ucDonVi1.lueDonVi.GetColumnValue("Ma_DT").ToString().Trim();
            MaDV = madv == "TC" ? '%' : Convert.ToChar(madv);
            TenDV = ucDonVi1.lueDonVi.GetColumnValue("Ten_DT").ToString().Trim();

            loadLueCanBo();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}